<?php
defined('BASEPATH') OR exit('No direct script access allowed');
//消息模板管理
class Msg_template extends CommonController {

	function __construct(){
		
		parent::__construct();
		$this->load->model('System/Msg_templateModel','CurModel');
		
		$this->load->model('Index/PrivgroupModel','PrivgroupModel');
		
	}
	
	
	//start
	/**
	* @name 获取列表
	* @param page ture char 页面
	* @param size ture char 数量
	* @return 成功返回1
	*/
	//end
	function get_list(){
		
		if(!$this->check_priv('notice_template|notice_template_1')){
			$this->err_out(LANG_NOPRIV);
		}
		

		$user_id = $this->user_id;
		
		$filter = array();
		$arr = array(
			'filter'=>$filter
		);
		
		$result = parent::_get_list($arr);
		if(!$result){
			$this->err_out($this->getError());
		}
		
		$this->data_out($result);
	}
	
	
	
	
	function info_out($info){
		
		
		$groups_data = array();
		$groups	= $this->PrivgroupModel->ids_get_data($info['groups']);
		if($groups){
			foreach($groups as $val){
				$groups_data[] = $val['name'];
			}
		}
		$info['groups_data'] = implode(',',$groups_data);
		
		return $info;
	}
	
	//start
	/**
	* @name 获取信息
	* @param id ture char ID
	* @return 成功返回1
	*/
	//end
	function get_info(){
		$user_id = $this->user_id;
		
		$result = parent::_get_info();
		if(!$result){
			$this->err_out($this->getError());
		}
		
		$this->data_out($result);
	}
	
	//start
	/**
	* @name 获取信息
	* @param id ture char ID
	* @return 成功返回1
	*/
	//end
	function save(){
		$user_id = $this->user_id;
		
		if(!$this->check_priv('notice_template_2')){
			$this->err_out(LANG_NOPRIV);
		}
		
		$filter = array(
			
		);
		$post = array('sys_is_send','sys_title','sys_content','groups','ggao_is_send','ggao_title','ggao_content');
		$farr = array(
			'post'=>$post,
			'filter'=>$filter
		);
		$time = time();
		
		$arr = array(
			'user_id'=>$user_id,
			'addtime'=>$time,
			'type'=>1
		);
		
		$result = parent::_save($farr,$arr);
		if(!$result){
			$this->err_out($this->getError());
		}
		
		$this->add_log("发送站内信息【{$result['id']}】",'bumen');
		$this->data_out($result);
	}
	//start
	/**
	* @name 更新对象
	* @param id ture int ID
	* @param ids ture char 权限组对象
	* @return 成功返回1
	*/
	//end
	function update_target(){
		
		if(!$this->check_priv('notice_template_2')){
			$this->err_out(LANG_NOPRIV);
		}
		
		$id = $this->input->post('id',null,0);
		$ids = $this->input->post('ids',null,'');
		if(!is_array($ids)){
			$ids = explode(',',$ids);
		}
		$info = $this->CurModel->get_info($id);
		if(!$info){
			$this->err_out('数据不存在');
		}
		
		$groups = implode(',',$ids);
		
		$this->CurModel->edit(array('groups'=>$groups),"id='{$id}'");
		
		$this->data_out(1);
	}
	
	
	//start
	/**
	* @name 删除
	* @param id ture int ID
	* @return 成功返回1
	*/
	//end
	function del(){
		
		if(!$this->check_priv('notice_template_2')){
			$this->err_out(LANG_NOPRIV);
		}
		
		$result = parent::_del();
		if(!$result){
			$this->err_out($this->getError());
		}
		
		
		$this->add_log("删除主体公司【{$result}】",'bumen');
		$this->data_out($result);
		
	}
	
	//start
	/**
	* @name 批量操作
	* @param ids ture int_array ID组合
	* @param act ture int 操作类型enable，disconnected，delete
	* @return 成功返回1
	*/
	//end
	function batch_act(){
		if(!$this->check_priv('notice_template_2')){
			$this->err_out(LANG_NOPRIV);
		}
		
		$ids = $this->input->post('ids',null,array());
		$act = $this->input->post('act',null,'');
		
		$act_arr = array('no_send');
		if(!in_array($act,$act_arr)){
			$this->err_out('错误的错误');
		}
		if(!is_array($ids)){
			$ids = explode(',',$ids);
		}
		if(count($ids)<=0){
			$this->err_out('请选择成员');
		}
		$ls_ids = array();
		foreach($ids as $id){
			if(!empty($id)){
				$ls_ids[] = $id;
			}
		}
		
		if($act=='no_send'){
			$this->CurModel->edit(array('sys_is_send'=>0,'ggao_is_send'=>0,),"id in(".implode(',',$ls_ids).")");
		}
		
		$this->data_out(1);
	}
	//start
	/**
	* @name 设置是否启用
	* @param id ture int ID
	* @param status ture int 状态
	* @return 成功返回1
	*/
	//end
	function set_enable(){
		
		if(!$this->check_priv('notice_template_2')){
			$this->err_out(LANG_NOPRIV);
		}
		
		$id = $this->input->post('id',null,0);
		$status = $this->input->post('status',null,0);
		$info = $this->CurModel->get_info($id);
		if(!$info){
			$this->err_out('数据不存在');
		}
		
		$this->CurModel->edit(array('is_enable'=>$status),"id ='{$id}'");
		
		$this->data_out(1);
	}
	
	
}
